//
//  ACLoginViewController.m
//  ACLiteAVUISDK_Example
//
//  Created by 冯琰琰 on 2021/3/29.
//  Copyright © 2021 fyanyan. All rights reserved.
//

#import "ACLoginViewController.h"
#import <ReactiveObjC.h>
#import "ACVertifyCodeViewController.h"
//#import <mach/mach.h>

@interface ACLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIView *line;

@property (weak, nonatomic) IBOutlet UIButton *getVertifyCode;

@end

@implementation ACLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.phoneTextField.borderStyle = UITextBorderStyleNone;
    [self.phoneTextField setPlaceHolderTextFont:[UIFont systemFontOfSize:20.0]];
    [self.phoneTextField setPlaceHolderTextColor:UIColorFromRGB(0xC0C4CC)];
    self.getVertifyCode.layer.masksToBounds = YES;
    self.getVertifyCode.layer.cornerRadius = 24;

    @weakify(self);
    RAC(self.getVertifyCode,enabled) = [[RACObserve(self.phoneTextField, text) merge:self.phoneTextField.rac_textSignal] map:^id _Nullable(NSString*  _Nullable value) {
        NSString *Str = [NSString stringWithString:[value stringByReplacingOccurrencesOfString:@" " withString:@""]];
        return  @(value.length == 13 && [Str isValidPhoneStr]);
    }];
    
    [[self.phoneTextField rac_textSignal] subscribeNext:^(NSString * _Nullable value) {
        @strongify(self)
        NSMutableString *str = [NSMutableString stringWithString:[value stringByReplacingOccurrencesOfString:@" " withString:@""]];
        if (value.length > 3 ) {
            [str insertString:@" " atIndex:3];
        }
        if (value.length > 8) {
            [str insertString:@" " atIndex:8];
        }
        self.phoneTextField.text = str;
    }];
    
    [[self.getVertifyCode rac_valuesForKeyPath:@"enabled" observer:self] subscribeNext:^(NSNumber *  _Nullable x) {
        @strongify(self)
        if (x == [NSNumber numberWithBool:YES]) {
            [self.getVertifyCode setBackgroundColor:UIColorFromRGB(0xFF6048)];
        }else{
            [self.getVertifyCode setBackgroundColor:UIColorFromRGB(0xFEAFA3)];
        }
    }];
}


- (IBAction)getVertifyCodeClick:(id)sender {
    [SVProgressHUD showWithStatus:@""];
    [ACLoginManager requestVertifyCodeWithPhone:[self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] requestCallback:^(int code, NSString * _Nullable message) {
        if (code == 0) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showInfoWithStatus:@"您的验证码为：888888"];
            ACVertifyCodeViewController *vertifyCodeVC = [[ACVertifyCodeViewController alloc] initWithNibName:@"ACVertifyCodeViewController" bundle:nil];
            vertifyCodeVC.phoneNum = self.phoneTextField.text;
            [self.navigationController pushViewController:vertifyCodeVC animated:YES];
        }else{
            [SVProgressHUD dismiss];
            [SVProgressHUD showInfoWithStatus:@"验证码获取失败"];
        }
    }];
//    UIGraphicsBeginImageContext(self.view.bounds.size);
//    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
//
//    UIView *snapView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
//    [self.view addSubview:snapView];
//    [snapView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(0);
//    }];
    
//    struct mach_task_basic_info info;
//    mach_msg_type_number_t size = sizeof(info);
//    kern_return_t kl = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &size);
//    if(kl != KERN_SUCCESS)
//    {
//        DDLogInfo(@"%d---------",kl);
//    }
//    DDLogInfo(@"-------当前系统占用的内存：%f单位:MB",(info.resident_size / 1024.0) / 1024.0) ;
//
//    vm_statistics_data_t vmStatus;
//    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
//    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStatus, &infoCount);
//    if (kernReturn != KERN_SUCCESS) {
//        DDLogInfo(@"%d----------",kernReturn);
//    }
//    DDLogInfo(@"-------可用内存：%f单位:MB",((vm_page_size * vmStatus.free_count) / 1024.0) / 1024.0) ;
}
@end
